home *** CD-ROM | disk | FTP | other *** search
- Path: info.uah.edu!oreo!gbacon
- From: gbacon@oreo (Greg Bacon)
- Newsgroups: comp.lang.c
- Subject: Re: Please HELP with struct and linked list
- Date: 4 Apr 1996 17:18:40 GMT
- Organization: The University of Alabama in Huntsville
- Message-ID: <4k109g$6mv@info.uah.edu>
- References: <31630C5A.53E6@soleil.acomp.usf.edu>
- Reply-To: gbacon@CS.UAH.Edu
- NNTP-Posting-Host: oreo.aspire.cs.uah.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Chris Frenck (cfrenck@soleil.acomp.usf.edu) wrote:
- : Could anyone PLEASE help me. I am having a great deal of difficulty
- : understanding the concepts of the following.
-
- : struct Class
- : {
- : char prefix;
- : char number;
- : char instructor;
- : char grade;
- : int credits;
- : struct Class *next; /*???*/
- : };
-
- : struct student
- : {
- : char fname;
- : char lname;
- : struct Record *next *previous;
- : struct Class *next_class; /*???*/
- : };
-
- : The two main areas I am having extreme difficulties in are forming a
- : linked list of students, sorting the list alphabetically by last name.
- : I also need to add more than one class per student. If anyone could help
- : me get over this hurdle I would be very happy.
-
- : I am having problems with the syntax of just how to do it. Does anyone
- : have an example of a program that does something similar? Or could someone
- : give me a "skeleton" of how to do it????
-
- : I have to do many other things with this program, but cannot continue
- : until I figure this problem out.
-
- : PLEASE help an honest, hard-working college student out!
-
- : Sincerely,
-
- : Chris Frenck
- : cfrenck@soleil.acomp.usf.edu
-
- OK.. this is how I'm interpreting your spec. You want something like a
- schedule for each student (with the list of students sorted alphabetically)
- cam sa:
-
- Student1 --> class list
- |
- Student2 --> class list
- :
- Student_{\lambda} --> class list
-
-
- One way to do that would be to have structs similar to these:
-
- struct _class_tag
- {
- char prefix[PREFIXLEN];
- char number[NUMBERLEN];
- char instructor[INSTRUCTORLEN];
- char grade;
- int credits;
-
- struct _class_tag *next;
- };
-
- struct _student_tag
- {
- char fname[NAMELEN];
- char lname[NAMELEN];
-
- struct _class_tag *classes;
- struct _student_tag *next;
- };
-
- Note that an object of type char can hold only a single character. I
- suspect you want fname, lname, et al to be arrays of char. You'll
- want to use the preprocessor #define to define the values of the
- length constants ([A-Z][A-Z]*LEN), i.e.,
-
- #define NAMELEN 15
-
- That way, you avoid lots of calls to the allocator and grab it all
- in one shot (with a minor inefficiency in storage... ahh, the good ol'
- space-time tradeoff).
-
- To save typing (no pun intended), you might consider a couple of
- typedefs, i.e.,
-
- typedef struct _class_tag Class;
- typedef struct _student_tag Student;
-
- If your compiler compiles C and C++, be careful about using 'class' (note
- the case); the compiler is likely to get horribly confused.
-
- From there, your task is just to maintain your list of students and
- append classes to the proper student. I have no idea what you were
- trying to do with 'struct Record'. If it had some significance, you
- didn't make it clear.
-
- Hope this helps,
- Greg
- --
- Greg Bacon <gbacon@cs.uah.edu>
- University of Alabama in Huntsville
- CS Department Systems Support Team
-